var app = angular.module('BaseServer');

app.factory('BaseService', ['$resource', '$http', 'PublicFunction', '$injector',
    function($resource, $http, PublicFunction, $injector) {
        return {
            //获取可扩展枚举
            getExtendEnum: function(enumCode, successHandle, errHandle){
                var parameter = {
                    Code: enumCode
                };

                PublicFunction.request('/api/base/getExtendEnum', false, parameter, successHandle, errHandle);
            },
            //获取枚举--通用，普通枚举与扩展枚举的判断用是否包含"."
            getEnum: function(enumCode, successHandle, errHandle){
                if(enumCode.indexOf (".") > -1){
                    var ary = enumCode.split(".");
                    try{
                        var enum1 = $injector.get(ary[0])[ary[1]];
                        if(enum1){
                            successHandle(enum1);
                        }
                        else{
                            var newErr = {
                                leval: 5,
                                title: '获取枚举失败',
                                message: enumCode + '：枚举不存在'
                            }
                            PublicFunction.errFun(newErr);
                        }
                    }
                    catch(err){
                        var newErr = {
                            leval: 5,
                            title: err.name,
                            message: err.message
                        }
                        PublicFunction.errFun(newErr);
                    }
                }
                else{
                    this.getExtendEnum(enumCode, successHandle, errHandle);
                }
            },
            //获取系统参数设置
            getSysParameterValue: function(parameterCode, successHandle, errHandle){
                var parameter = {
                    Code: parameterCode
                };

                PublicFunction.request('/api/base/getSysParameterValue', false, parameter, successHandle, errHandle);
            },
            downloadFile: function(serverPath, downloadPath, downloadFileName, uploadPath, newFileName, deleteFileName, socketId, successHandle, errHandle){
                var parameter = {
                    ServerPath: serverPath,
                    DownloadPath: downloadPath,
                    DownloadFileName: downloadFileName,

                    UploadPath: uploadPath,
                    FileName: newFileName,                      
                    DeleteFileName: deleteFileName,
                    SocketId: socketId
                };

                PublicFunction.request('/api/base/downloadFile', false, parameter, successHandle, errHandle);
            },
            fileRenames: function(filePaths, successHandle){
                var parameter = {
                    FilePaths: filePaths
                };

                PublicFunction.request('/api/base/fileRenames', false, parameter, successHandle, errHandle);
            },
            deleteFiles: function(filePaths, successHandle){
                var parameter = {
                    FilePaths: filePaths
                };

                PublicFunction.request('api/base/fileDeletes', false, parameter, successHandle, errHandle);
            }
        };
    }
]);

